<template>
  <a-spin :spinning="confirmLoading" class="spin-style">
    <j-form-container :disabled="formDisabled"  style="width: 100%;">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="24">
          <a-col :span="10">
            <a-row>
              <a-col>
                <a-form-model-item label="经度坐标" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lng">
                  <a-input v-model="model.lng" placeholder="请输入经度坐标"></a-input>
                </a-form-model-item>
              </a-col>
              <a-col>
                <a-form-model-item label="纬度坐标" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lat">
                  <a-input v-model="model.lat" placeholder="请输入纬度坐标"></a-input>
                </a-form-model-item>
              </a-col>
              <a-col>
                <span class="tip-label-style">*请点击地图上的位置，获取设备坐标信息，可手动修改</span>
              </a-col>
              <a-col>
                <a-form-model-item label="设备编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deviceId">
                  <a-input v-model="model.deviceId" placeholder="请输入设备编号"  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col>
                <a-form-model-item label="设备序列号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serialNumber">
                  <a-input v-model="model.serialNumber" placeholder="请输入设备序列号"  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col>
                <a-form-model-item label="设备ip" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deviceIp">
                  <a-input v-model="model.deviceIp" placeholder="请输入设备ip"  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col>
                <a-form-model-item label="设备厂商" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="manufacturerId">
                  <!-- <a-input v-model="model.manufacturerId" placeholder="请输入设备厂商"  ></a-input> -->
                  <j-dict-select-tag style="width: 100%;" v-model="model.manufacturerId" title="请输入设备厂商" dictCode="device_manufacturer" placeholder="请输入设备厂商"/>
                </a-form-model-item>
              </a-col>
              <a-col></a-col>

              <a-col>
                <a-form-model-item label="厂商设备名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="manufacturerDeviceName">
                  <a-input v-model="model.manufacturerDeviceName" placeholder="请输入厂商设备名称"  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col>
                <a-form-model-item label="设备名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deviceName">
                  <a-input v-model="model.deviceName" placeholder="请输入设备名称"  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col>
                <a-form-model-item label="RSU型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deviceModel">
                  <a-input v-model="model.deviceModel" placeholder="请输入RSU型号"  ></a-input>
                </a-form-model-item> 
              </a-col>

              <a-col>
                <a-form-model-item label="联网状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-radio-group name="onlineState" v-model="model.onlineState">
                    <a-radio :value="1">在线</a-radio>
                    <a-radio :value="0">离线</a-radio>
                  </a-radio-group>
                </a-form-model-item>
              </a-col>
              <a-col>
                <a-form-model-item label="所属路口" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="crossId">
                  <a-select v-model="model.crossId" title="请选择所属路口" :options="crossOptions" @change="handleCrossChange" placeholder="请选择所属路口"/>
                </a-form-model-item>
              </a-col>
              <a-col>
                <a-form-model-item label="路侧杆件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="poleId">
                  <a-select v-model="model.poleId" title="请选择路侧杆件" :options="poleOptions" @change="handlePoleChange" placeholder="请选择路侧杆件"/>
                </a-form-model-item>
              </a-col>
              <a-col>
                <a-form-model-item label="所属道路" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="roadId">
                  <a-select style="width: 100%;" v-model="model.roadId" title="所属道路" :options="roadOptions" placeholder="所属道路" disabled/>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-col>
          <a-col :span="14">
            <a-row>
              <div style="width: 600px; height: 800px">
                <!-- <div id="map_canvas" style="width: 500px; height: 500px"></div> -->
                <BMap :pageHeight="550" ref="map" :zoom="12" @update-lnglat="updateLnglat"></BMap>
              </div>
            </a-row>
            
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
  import BMap from '@/views/components/map/index.vue'
  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import { getRoadList } from '@/api/basicinfo/road/road'
  import { getCrossList } from '@/api/basicinfo/road/cross'
  import { getPoleList } from '@/api/basicinfo/roadside/pole'

  export default {
    name: 'RsuForm',
    components: {
      BMap,
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          deviceId: [
              { required: true, message: '请输入设备编号!'},
           ],
          serialNumber: [
              { required: true, message: '请输入设备序列号!'},
           ],
           channelNumber: [
              { required: true, message: '请输入通道编号!'},
           ],
           internationalCoding: [
              { required: true, message: '请选择国际编码!'},
           ],
           deviceIp: [
              { required: true, message: '请输入设备ip!'},
           ],
           manufacturerId: [
              { required: true, message: '请选择设备厂商!'},
           ],
           manufacturerDeviceName: [
              { required: true, message: '请输入厂商设备名称!'},
           ],
           deviceName: [
              { required: true, message: '请输入设备名称!'},
           ],
           streamAddr: [
              { required: true, message: '请输入推流地址!'},
           ],
           deviceModel: [
              { required: true, message: '请输入设备型号!'},
           ],
           deviceType: [
              { required: true, message: '请选择设备类型!'},
           ],
           onlineState: [
              { required: true, message: '请输入联网状态!'},
           ],
           roadId: [
              { required: true, message: '请选择所属道路!'},
           ],
           crossId: [
              { required: true, message: '请选择所属路口!'},
           ],
           poleId: [
              { required: true, message: '请选择路侧杆件!'},
           ],
        },
        url: {
          add: "/basicinfo/roadsideDevice/rsu/add",
          edit: "/basicinfo/roadsideDevice/rsu/edit",
          queryById: "/basicinfo/roadsideDevice/rsu/queryById"
        },
        crossOptions: [],
        roadOptions: [],
        crossList: [],
        currentRoadId: null,
        currentCrossId: null,
        poleOptions: [],
        markerImagePath: 'assets/operationalSupervision/map/point-icon1.png',
        markerImageWidth: 34,
        markerImageHeight: 40,
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.initRoadList(); 
      this.initCrossList();
      this.initPoleList();
    },
    methods: {
      add () {
        const that = this;
        
        this.edit(this.modelDefault);
        this.model = {
          lng: null,
          lat: null,
          poleId: null
        };
        let lnglat = {"lng":this.$refs.map.mapCenter.lng, "lat":this.$refs.map.mapCenter.lat};
        this.$refs.map.setCenterAndZoom(lnglat, this.$refs.map.zoom);
        this.$refs.map.onclickEventByAddMarker(that.markerImagePath, that.markerImageWidth, that.markerImageHeight, (e)=>{
          console.log(e);
        })
      },
      edit (record) {
        const that = this;
        this.model = Object.assign({}, record);
        this.visible = true;
        // let lnglat = {"lng":record.lng, "lat":record.lat};
        // const item = {"name": record.deviceId, "sx": 0, "zy": 0, "scale":0.5, "color": "#000000", "bg": "point-pole"}
        // this.$refs.map.addMarker(lnglat, item, record.id);
        const lnglat = {"lng": record.lng, "lat": record.lat};
        this.$refs.map.addImageMarker(lnglat, {iconPath: that.markerImagePath, width: that.markerImageWidth, height: that.markerImageHeight});
        this.$refs.map.setCenterAndZoom(lnglat, this.$refs.map.zoom);
        this.$refs.map.onclickEventByAddMarker(that.markerImagePath, that.markerImageWidth, that.markerImageHeight, (e)=>{
          console.log(e);
        })
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
       //初始化道路字典
      initRoadList(){
        getRoadList().then((res)=>{
          if(res.success){
            this.roadOptions = res.result.map((item,index,arr)=>{
              let c = { label:item.roadName, value:item.id }
              return c;
            })
            // console.log('this.roadOptions: ',this.roadOptions)
          }
        });
      },
      //初始化路口字典
      initCrossList(){
        let param = {};
        this.queryCrossList(param);
      },
      queryCrossList(param){
        getCrossList(param).then((res)=>{
          if(res.success){
            this.crossList = res.result;
            this.crossOptions = res.result.map((item,index,arr)=>{
              let c = { label:item.name, value:item.id}
              return c;
            })
            console.log('this.crossOptions: ',this.crossOptions);
          }
        });
      },
      handleCrossChange(val){
        console.log('this.crossOptions: ',this.crossOptions);
        console.log('this.model: ',this.model);
        console.log('val: ', val);
        for(let i = 0 ,length = this.crossList.length; i<length; i++){
          if(this.crossList[i].id === val) {
            this.model.crossName = this.crossList[i].name;
            this.model.roadId = this.crossList[i].roadId;
            
            break;
          }
        }
        this.model.poleId = null;
        this.poleOptions = [];
        this.queryPoleList({crossId: val});
        console.log('this.model.poleId: ',this.model.poleId);
        console.log('this.crossOptions: ',this.crossOptions);
        console.log('this.model: ',this.model);
      },
      initPoleList(){
        let param = {};
        this.queryPoleList(param);
      },
      queryPoleList(param){
        getPoleList(param).then((res)=>{
          if(res.success){
            this.poleOptions = res.result.map((item,index,arr)=>{
              let c = { label: item.crossName + '-' + item.positionTypeText, value:item.id }
              return c;
            })
            console.log('this.poleOptions: ',this.poleOptions)
          }
        });
      },
      handlePoleChange(val){
        console.log('poleId val: ', val);
        console.log('poleId: ', this.model.poleId);
        console.log('poleOptions: ', this.poleOptions);
        
      },
      updateLnglat(latlng){
        console.log('latlng: ', latlng);
        // console.log('this: ', this);
        console.log('this.model: ', this.model);
        this.model.lng = latlng.lng;
        this.model.lat = latlng.lat;        
      }
    }
  }
</script>
<style scoped>
  div.ant-modal, div.ant-modal-content {
      width: 1400px;
  }
  .spin-style{
      width: 1200px;
  }
  .tip-label-style{
    display: inline;
    width: 100%;
    text-align: center;
    font-size: 10px;
    color: red;
  }
  .area-select.medium{
    width: 100px;
  }
</style>